import java.util.Random;

import lejos.nxt.*;

public class TestController extends Thread {
	private DistanceMonitor frontMonitor;
	private DistanceMonitor rearMonitor;
	private DistanceMonitor sideMonitor;

	public TestController(DistanceMonitor frontMonitor,
			DistanceMonitor rearMonitor, DistanceMonitor sideMonitor) {
		this.frontMonitor = frontMonitor;
		this.rearMonitor = rearMonitor;
		this.sideMonitor = sideMonitor;
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Motor.A.setSpeed(360);
			if (Button.ENTER.isPressed()) {
				System.exit(0);
			}
			if(Button.ESCAPE.isPressed()) {
				Motor.A.setSpeed(0);
			}
			if (frontMonitor.getDistance() < 10) {
				Motor.A.backward();
				Motor.B.rotateTo(0, true);
			}
			if (rearMonitor.getDistance() < 10) {
				Motor.A.forward();
				Motor.B.rotateTo(0, true);
			}
			if (sideMonitor.getDistance() < 10) {
				Random r = new Random();
				if (r.nextInt(2) == 0) {
					Motor.B.rotateTo(35, true);
				} else {
					Motor.B.rotateTo(-35, true);
				}

			}
		}

	}
}